package com.nuoyun.tnvmue.activity

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.nuoyun.tnvmue.R
import com.nuoyun.tnvmue.utils.LogUtil
import com.nuoyun.tnvmue.utils.ToolUtils

/**
 * @author JiangAdmin
 * date: 2020/4/10
 * Email: jiangmr@vip.qq.com
 * TODO: 我的
 */
open class BaseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //把状态栏设置为透明
        val window = window
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        //透明状态栏
        window.statusBarColor = Color.TRANSPARENT
    }

    /**
     * ToolBar样式
     *
     * @param type 0 白  1 左右渐变变 2 上下渐变
     */
    fun toolBarStyle(type: Int) {
        when (type) {
            -1 -> {
                findViewById<View>(R.id.view_title_bar).setBackgroundResource(R.color.transparent)
                (findViewById<View>(R.id.title) as TextView).setTextColor(ContextCompat.getColor(this, R.color.white))
                (findViewById<View>(R.id.back) as ImageButton).setImageResource(R.drawable.ic_back)
                (findViewById<View>(R.id.menu) as TextView).setTextColor(ContextCompat.getColor(this, R.color.white))
                setDarkStatusIcon(false)
            }
            0 -> {
                findViewById<View>(R.id.view_title_bar).setBackgroundColor(ContextCompat.getColor(this, R.color.white))
                (findViewById<View>(R.id.title) as TextView).setTextColor(ContextCompat.getColor(this, R.color.gray_3))
                (findViewById<View>(R.id.back) as ImageButton).setImageResource(R.drawable.ic_back_g)
                (findViewById<View>(R.id.menu) as TextView).setTextColor(ContextCompat.getColor(this, R.color.gray_3))
                setDarkStatusIcon(true)
            }
            1 -> {
                findViewById<View>(R.id.view_title_bar).setBackgroundResource(R.color.style_color)
                (findViewById<View>(R.id.title) as TextView).setTextColor(ContextCompat.getColor(this, R.color.white))
                (findViewById<View>(R.id.back) as ImageButton).setImageResource(R.drawable.ic_back)
                (findViewById<View>(R.id.menu) as TextView).setTextColor(ContextCompat.getColor(this, R.color.white))
                setDarkStatusIcon(false)
            }
            else -> {
            }
        }
    }

    /**
     * 设置状态栏颜色
     *
     * @param color
     */
    fun setDarkStatusColor(color: Int) {
        try {
            val window = window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = ContextCompat.getColor(this, color)
            //底部导航栏
            //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 状态栏 亮暗
     *
     * @param bDark
     */
    fun setDarkStatusIcon(bDark: Boolean) {

        //如果是Android 6.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val decorView = window.decorView
            var vis = decorView.systemUiVisibility
            vis = if (bDark) {
                vis or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                vis and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
            }
            decorView.systemUiVisibility = vis
        }
        val deviceMan = Build.MANUFACTURER
        if ("Xiaomi" == deviceMan) {
            setMiuiStatusBarDarkMode(this, bDark)
        } else if ("meizu" == deviceMan) {
            setMeizuStatusBarDarkIcon(this, bDark)
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    fun setTitle(title: String?) {
        val titlebar = findViewById<RelativeLayout>(R.id.view_title_bar)
        val layoutParams = titlebar.layoutParams
        //获取状态栏高度 加上 要设置的标题栏高度 等于 标题栏实际高度
        layoutParams.height = ToolUtils.getStatusHeight() + ToolUtils.dp2px(44f)
        titlebar.layoutParams = layoutParams
        val tv = findViewById<TextView>(R.id.title)
        tv.text = title
    }

    /**
     * 设置标题
     *
     * @param title
     */
    override fun setTitle(title: Int) {
        val titlebar = findViewById<RelativeLayout>(R.id.view_title_bar)
        val layoutParams = titlebar.layoutParams as LinearLayout.LayoutParams
        //获取状态栏高度 加上 要设置的标题栏高度 等于 标题栏实际高度
        layoutParams.height = ToolUtils.getStatusHeight() + ToolUtils.dp2px(44f)
        titlebar.layoutParams = layoutParams
        val tv = findViewById<TextView>(R.id.title)
        tv.text = resources.getString(title)
    }

    /**
     * 是否有返回键
     *
     * @param isvisible
     */
    fun setBack(isvisible: Boolean) {

//        if (RightOut && !SaveUtils.getBoolean(SaveKey.S_关闭手势)) {
//            new SlidingLayout(this).bindActivity(this);
//        }
        val backBtn = findViewById<ImageButton>(R.id.back)
        if (isvisible) {
            backBtn.visibility = View.VISIBLE
        } else {
            backBtn.visibility = View.GONE
        }
        backBtn.setOnClickListener { v: View? -> onBackPressed() }
    }

    /**
     * 设置菜单
     *
     * @param objects 图片或者文字/文字或者颜色/颜色
     */
    fun setMenu(vararg objects: Any?) {
        MENU = findViewById(R.id.menu)
        if (objects.isNotEmpty()) {
            MENU!!.visibility = (View.VISIBLE)
            MENU!!.setOnClickListener(this as View.OnClickListener)
            //判断是不是图
            if (objects[0] is Int) {
                val drawableleft = ContextCompat.getDrawable(this, (objects[0] as Int?)!!)
                drawableleft!!.setBounds(0, 0, drawableleft.intrinsicWidth, drawableleft.minimumHeight)
                //只放左边
                MENU!!.setCompoundDrawables(drawableleft, null, null, null)

                //判断是不是字
                if (objects.size > 1 && objects[1] is String) {
                    MENU!!.setText(objects[1] as String?)

                    //判断是不是颜色
                    if (objects.size > 2 && objects[2] is Int) {
                        MENU!!.setTextColor(ContextCompat.getColor(this, (objects[2] as Int?)!!))
                    }
                }
            }

            //判断是不是字
            if (objects[0] is String) {
                MENU!!.setText(objects[0] as String?)
                //判断是不是颜色
                if (objects.size > 1 && objects[1] is Int) {
                    MENU!!.setTextColor(ContextCompat.getColor(this, (objects[1] as Int?)!!))
                }
            }
        } else {
            MENU!!.visibility = View.GONE
        }
    }

    companion object {
        private const val TAG = "BaseActivity"

        /**
         * MIUI 系统
         *
         * @param activity
         * @param darkmode 字体颜色为黑色。
         * @return
         */
        fun setMiuiStatusBarDarkMode(activity: Activity, darkmode: Boolean): Boolean {
            val clazz: Class<out Window> = activity.window.javaClass
            try {
                var darkModeFlag = 0
                val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
                val field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE")
                darkModeFlag = field.getInt(layoutParams)
                val extraFlagField = clazz.getMethod("setExtraFlags", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
                extraFlagField.invoke(activity.window, if (darkmode) darkModeFlag else 0, darkModeFlag)
                return true
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return false
        }

        /**
         * Flyme 系统
         *
         * @param activity
         * @param dark     字体颜色
         * @return
         */
        fun setMeizuStatusBarDarkIcon(activity: Activity?, dark: Boolean): Boolean {
            var result = false
            if (activity != null) {
                try {
                    val lp = activity.window.attributes
                    val darkFlag = WindowManager.LayoutParams::class.java
                            .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON")
                    val meizuFlags = WindowManager.LayoutParams::class.java
                            .getDeclaredField("meizuFlags")
                    darkFlag.isAccessible = true
                    meizuFlags.isAccessible = true
                    val bit = darkFlag.getInt(null)
                    var value = meizuFlags.getInt(lp)
                    value = if (dark) {
                        value or bit
                    } else {
                        value and bit.inv()
                    }
                    meizuFlags.setInt(lp, value)
                    activity.window.attributes = lp
                    result = true
                } catch (e: Exception) {
                    LogUtil.e(TAG, e.message)
                }
            }
            return result
        }

        var MENU: TextView? = null
    }
}